<?php
/**
 * Author：晴天 QQ：1186258278
 * Creation：2020/4/24 12:13
 * Filename：faka.php
 * 发卡商品
 */

namespace lib\supply;


use Medoo\DB\SQL;

class faka
{
    /**
     * @param $OrData 订单信息
     * @param $Goods 商品数据
     */
    public static function Submit($OrData, $Goods)
    {
        global $date;
        $DB = SQL::DB();
        $Res = $DB->select('token', ['kid', 'token'], [
            'gid' => $Goods['gid'],
            'endtime' => null,
            'ORDER' => 'kid',
            'LIMIT' => $Goods['num']
        ]);

        /**
         * 修改订单状态！
         */
        $Darr = [];
        $TokenArr = [];
        foreach ($Res as $v) {
            $Darr = array_merge($Darr, [$v['kid']]);
            $TokenArr = array_merge($TokenArr, [$v['token']]);
        }

        $Order = $DB->get('order', '*', ['id' => $OrData['id']]);

        $Re = $DB->update('token', [
            'uid' => $Order['uid'],
            'order' => $Order['order'],
            'code' => json_decode($Order['input'], TRUE)[0],
            'ip' => $Order['ip'],
            'endtime' => $date,
        ], [
            'kid' => $Darr
        ]);

        return [
            'code' => ($Re ? 1 : -1),
            'msg' => ($Re ? '发卡成功,本次共发卡' . count($Darr) . '张！,' . implode('|', $TokenArr) : '发卡失败,请联系管理员发卡！'),
            'order' => $Order['order'],
        ];

    }


}